package cn.edu.njust.alg;

public class Union_find {

	public static int[] parent = new int[1002];

	public static void init() {
		for (int i = 0; i < parent.length; i++) {
			parent[i] = i;
		}
	}

	public static int find(int x) {

		return x == parent[x]?x :(parent[x] = find(parent[x]));
		// int r = x;
		// while (parent[r] != r) {
		// r = parent[r];
		// }
		// int i = x, j;
		// while (i != r) {
		// j = parent[i];
		// parent[i] = r;
		// i = j;
		// }
		// return r;
	}

	public static void join(int x, int y) {
		int px = find(x);
		int py = find(y);
		if (px != py) {
			parent[px] = py;
		}
	}

	public static void main(String[] args) {

	}

}
